<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="用户ID" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户ID"></el-input>
    </el-form-item>
    <el-form-item label="mlsId" prop="mlsId">
      <el-input v-model="dataForm.mlsId" placeholder="mlsId"></el-input>
    </el-form-item>
    <el-form-item label="请求参数内容" prop="propertiesData">
      <el-input v-model="dataForm.propertiesData" placeholder="请求参数内容"></el-input>
    </el-form-item>
    <el-form-item label="收藏状态" prop="cltStatus">
      <el-input v-model="dataForm.cltStatus" placeholder="收藏状态"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          mcId: 0,
          createTime: '',
          userId: '',
          mlsId: '',
          propertiesData: '',
          cltStatus: ''
        },
        dataRule: {
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '用户ID不能为空', trigger: 'blur' }
          ],
          mlsId: [
            { required: true, message: 'mlsId不能为空', trigger: 'blur' }
          ],
          propertiesData: [
            { required: true, message: '请求参数内容不能为空', trigger: 'blur' }
          ],
          cltStatus: [
            { required: true, message: '收藏状态不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.mcId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.mcId) {
            this.$http({
              url: this.$http.adornUrl(`/memberhpr/mycollect/info/${this.dataForm.mcId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.createTime = data.mycollect.createTime
                this.dataForm.userId = data.mycollect.userId
                this.dataForm.mlsId = data.mycollect.mlsId
                this.dataForm.propertiesData = data.mycollect.propertiesData
                this.dataForm.cltStatus = data.mycollect.cltStatus
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        var _this = this
        _this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            _this.$http({
              url: _this.$http.adornUrl(`/memberhpr/mycollect/${!_this.dataForm.mcId ? 'save' : 'update'}`),
              method: 'post',
              data: _this.$http.adornData({
                'mcId': _this.dataForm.mcId || undefined,
                'createTime': _this.dataForm.createTime,
                'userId': _this.dataForm.userId,
                'mlsId': _this.dataForm.mlsId,
                'propertiesData': _this.dataForm.propertiesData,
                'cltStatus': _this.dataForm.cltStatus
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                _this.$message.success('success', 1, function () {
                  _this.visible = false
                  _this.$emit('refreshDataList')
                })
              } else {
                _this.$message.error(data.msg, 1)
              }
            })
          }
        })
      }
    }
  }
</script>
